package com.wiscamp.ninechapters.panels.application.assemblers;

import com.wiscamp.ninechapters.panels.application.vo.SimpleMenuVO;
import com.wiscamp.ninechapters.panels.application.vo.SimplePermissionVO;
import com.wiscamp.ninechapters.panels.application.vo.SimpleRoleVO;
import com.wiscamp.ninechapters.panels.application.vo.SimpleUserVO;
import com.wiscamp.ninechapters.panels.domain.repositories.MenuRepository;
import com.wiscamp.ninechapters.panels.domain.repositories.PermissionRepository;
import com.wiscamp.ninechapters.panels.domain.repositories.RoleRepository;
import com.wiscamp.ninechapters.panels.domain.repositories.UserRepository;
import cube.common.core.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

@Component
public class AssemblyLoader {

    @Autowired
    private static UserRepository userRepository;

    @Autowired
    private static RoleRepository roleRepository;

    @Autowired
    private static MenuRepository menuRepository;

    @Autowired
    private static PermissionRepository permissionRepository;

    public static Collection<SimpleUserVO> loadUsers(Collection<Long> userIds) {
        if (!CollectionUtils.isEmpty(userIds)) {
            var users = userRepository.getUsersByIds(userIds);
            return CollectionUtils.toArrayList(users, UserAssembler.Instance::toSimpleUserVO);
        }
        return new ArrayList<>();
    }

    public static Collection<SimpleRoleVO> loadRoles(Collection<Long> roleIds) {
        if (!CollectionUtils.isEmpty(roleIds)) {
            var roles = roleRepository.getRolesByIds(roleIds);
            return CollectionUtils.toArrayList(roles, RoleAssembler.Instance::toSimpleRoleVO);
        }
        return new ArrayList<>();
    }

    public static Collection<SimpleMenuVO> loadMenus(Collection<Long> menuIds) {
        if (!CollectionUtils.isEmpty(menuIds)) {
            var roles = menuRepository.getMenusByIds(menuIds);
            return CollectionUtils.toArrayList(roles, MenuAssembler.Instance::toSimpleMenuVO);
        }
        return new ArrayList<>();
    }

    public static Collection<SimplePermissionVO> loadPermissions(Collection<Long> permissionIds) {
        if (!CollectionUtils.isEmpty(permissionIds)) {
            var roles = permissionRepository.getPermissionsByIds(permissionIds);
            return CollectionUtils.toArrayList(roles, PermissionAssembler.Instance::toSimplePermissionVO);
        }
        return new ArrayList<>();
    }
}
